/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.framework.entity;

import cn.star.framework.annotation.ExcelCell;
import cn.star.framework.core.Constant;
import cn.star.framework.core.IEntity;
import cn.star.framework.type.Status;
import java.time.Instant;
import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;
import lombok.Data;
import org.hibernate.annotations.Comment;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

/**
 * 基础实体<br>
 * 提供基础的字段，但是不支持主键策略<br>
 * 支持审计{@link AuditorAwareImpl}<br>
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
@Data
@MappedSuperclass
@EntityListeners({AuditingEntityListener.class})
public abstract class AbstractBaseEntity implements IEntity {
  /** 租户Id */
  @Column(name = "fk_tenant_id_", length = 64)
  @Comment(value = "租户Id")
  @ExcelCell(name = "租户", comment = "租户Id")
  private String tenantId = Constant.EMPTY;

  /** 创建时间 */
  @CreatedDate
  @Column(name = "create_time_", updatable = false)
  @Comment(value = "创建时间")
  @ExcelCell(name = "创建时间")
  private Instant createTime;

  /** 修改时间 */
  @LastModifiedDate
  @Column(name = "update_time_")
  @Comment(value = "修改时间")
  @ExcelCell(name = "修改时间")
  private Instant updateTime;

  /**
   * 乐观锁<br>
   * 数据每次更新会自动递增<br>
   * 数据更新时候需要使用{@link #id}和{@link #version}进行更新，否则会出现异常：<br>
   * <span color='red'>optimistic locking failed; nested exception is
   * org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or
   * unsaved-value mapping was incorrect)</span>
   */
  @Version
  @Column(name = "version_")
  @Comment(value = "版本号、乐观锁")
  @ExcelCell(name = "版本号")
  private Integer version;

  /** 创建者 */
  @CreatedBy
  @Column(name = "create_user_", length = 64, updatable = false)
  @Comment(value = "创建者")
  @ExcelCell(name = "创建者")
  private String createUser;

  /** 最后修改者 */
  @LastModifiedBy
  @Column(name = "update_user_", length = 64)
  @Comment(value = "修改者")
  @ExcelCell(name = "修改者")
  private String updateUser;

  /** 数据状态<br */
  @Column(name = "status_", length = 32)
  @Comment(value = "数据状态")
  @ExcelCell(name = "状态")
  @Enumerated(EnumType.STRING)
  private Status status;
}
